#include "stdio.h"
#include "signal.h"
#include "pthread.h"
#include "stdlib.h"
#include "unistd.h"
#include "sys/types.h"
#include "string.h"

void sig_handler1(int arg)
{
    printf("thread1 get signal\n");
    return;
}


void sig_handler2(int arg)
{
    printf("thread2 get signal\n");
    return;
}


void * thread1_start(void *argv)
{
    printf("%s is created!\n", (char *)argv);

    struct sigaction act;
    memset(&act, 0, sizeof(act));

    sigaddset(&act.sa_mask, SIGQUIT);
    act.sa_handler = sig_handler1;
    sigaction(SIGQUIT, &act, NULL);

    pthread_sigmask(SIG_BLOCK, &act.sa_mask, NULL);

    sleep(2);
}


void * thread2_start(void *argv)
{
    printf("%s is created!\n", (char *)argv);

    struct sigaction act;
    memset(&act, 0, sizeof(act));

    sigaddset(&act.sa_mask, SIGQUIT);
    act.sa_handler = sig_handler2;
    sigaction(SIGQUIT, &act, NULL);

    //pthread_sigmask(SIG_BLOCK, &act.sa_mask, NULL);

    sleep(2);
}


int main()
{
    pthread_t tid1, tid2;
    int ret;

    ret = pthread_create(&tid1, NULL, thread1_start, "thread1");
    if(ret){
        printf("create thread1 failed\n");
        return ;
    }

    ret = pthread_create(&tid2, NULL, thread2_start, "thread2");
    if(ret){
        printf("create thread2 failed\n");
        return;
    }

    sleep(1);

    ret = pthread_kill(tid1, SIGQUIT);
    if(ret){
        printf("send signal to thread1 failed\n");
        return;
    }

    ret = pthread_kill(tid2, SIGQUIT);
    if(ret){
        printf("send signal to thread2 failed\n");
        return;
    }

    pthread_join(tid1, NULL);
    pthread_join(tid2, NULL);

    return 0;
}
